﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using PSGQuickTimeTracker.Properties;


namespace PSGQuickTimeTracker.DataModel
{
	[XmlRoot("QTTasks")]
	public class QTTasks : ObservableCollection<QTTask>
	{
		string XmlOutPath = Settings.Default.XmlOutFolderPath + @"\QTTasks.xml";


		public QTTasks()
		{			
		}

		
		public QTTasks(string xmlOutPath)
		{
			XmlOutPath = Path.Combine(xmlOutPath, "QTTasks.xml");			
		}

		

		public void ReloadTasks()
		{
			this.Clear();
			if (new FileInfo(XmlOutPath).Exists)
			{
				var existing = import();
				foreach (var item in existing)
				{
					this.Add(item);
				}
			}
		}


		
		public ObservableCollection<QTTask> import()
		{
			XmlSerializer deserializer = new XmlSerializer(this.GetType());
			ObservableCollection<QTTask> taskList;
			using (TextReader tr = new StreamReader(XmlOutPath))
			{
				taskList = (ObservableCollection<QTTask>)deserializer.Deserialize(tr);
			}
			return taskList;
		}



		public void export()
		{
			XmlSerializer serializer = new XmlSerializer(typeof(QTTasks));
			using (TextWriter tw = new StreamWriter(XmlOutPath))
			{
				serializer.Serialize(tw, this);
			}
		}
	}

}
